import { Provide, Inject } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { SpatialFeature } from '../entity/spatialFeatures.entity'; // 导入 SpatialFeature 实体
import { SpatialFeatureCreateDto, SpatialFeatureUpdateDto, SpatialFeatureQueryDto, SpatialFeatureDeleteDto } from '../dto/spatialFeatures.dto'; // 导入 DTO

@Provide()
export class SpatialFeatureService {

  @InjectEntityModel(SpatialFeature)
  private readonly spatialFeatureRepository: Repository<SpatialFeature>; // 注入 SpatialFeature 的 Repository

  // 添加空间特征
  async addSpatialFeature(params: SpatialFeatureCreateDto) {
    try {
      const spatialFeature = new SpatialFeature();
      spatialFeature.TBID = params.TBID;
      spatialFeature.Area = params.Area;
      spatialFeature.JCRWMC = params.JCRWMC;
      spatialFeature.JCRWDM = params.JCRWDM;
      spatialFeature.TBDM = params.TBDM;
      spatialFeature.YSWTLXMC = params.YSWTLXMC;
      spatialFeature.YSWTLXDM = params.YSWTLXDM;
      spatialFeature.TBBHLX = params.TBBHLX;
      spatialFeature.JCRQ = params.JCRQ;
      spatialFeature.JZRQ = params.JZRQ;
      spatialFeature.JD = params.JD;
      spatialFeature.WD = params.WD;
      spatialFeature.SHENG = params.SHENG;
      spatialFeature.SHI = params.SHI;
      spatialFeature.XIAN = params.XIAN;
      spatialFeature.XZQDM = params.XZQDM;
      spatialFeature.WTFXRQ = params.WTFXRQ;
      spatialFeature.ZYRY = params.ZYRY;
      spatialFeature.ZJRY = params.ZJRY;
      spatialFeature.WTLY = params.WTLY;
      spatialFeature.BZ = params.BZ;
      spatialFeature.JZSXWXSJ = params.JZSXWXSJ;     
      spatialFeature.JCSXWXSJ = params.JCSXWXSJ;     
      spatialFeature.geom = params.geom;

      const result = await this.spatialFeatureRepository.save(spatialFeature);
      return {
        success: true,
        data: result,
        message: '新增空间特征成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '新增空间特征失败: ' + error.message,
      };
    }
  }

  // 根据查询条件获取空间特征信息，支持分页
  async findSpatialFeatures(query: SpatialFeatureQueryDto) {
    const { TBID, JCRWMC, page = 1, limit = 10 } = query;
    const where: any = {};

    if (TBID) {
      where['TBID'] = TBID;
    }

    if (JCRWMC) {
      where['JCRWMC'] = JCRWMC;
    }

    try {
      const [spatialFeatures, total] = await this.spatialFeatureRepository.findAndCount({
        where,
        skip: (page - 1) * limit,  // 跳过的条数
        take: limit,               // 每页返回的条数
      });

      return {
        success: true,
        data: spatialFeatures,
        total,                      // 返回总条数
        message: '查询空间特征成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '查询空间特征失败: ' + error.message,
      };
    }
  }

  // 更新空间特征
  async updateSpatialFeature(params: SpatialFeatureUpdateDto) {
    const { id } = params;
    try {
      const spatialFeature = await this.spatialFeatureRepository.findOne({
        where: { id },
      });
      if (!spatialFeature) {
        return {
          success: false,
          message: '要更新的空间特征不存在',
        };
      }
      spatialFeature.TBID = params.TBID ?? spatialFeature.TBID;
      spatialFeature.Area = params.Area ?? spatialFeature.Area;
      spatialFeature.JCRWMC = params.JCRWMC ?? spatialFeature.JCRWMC;
      spatialFeature.JCRWDM = params.JCRWDM ?? spatialFeature.JCRWDM;
      spatialFeature.TBDM = params.TBDM ?? spatialFeature.TBDM;
      spatialFeature.YSWTLXMC = params.YSWTLXMC ?? spatialFeature.YSWTLXMC;
      spatialFeature.YSWTLXDM = params.YSWTLXDM ?? spatialFeature.YSWTLXDM;
      spatialFeature.TBBHLX = params.TBBHLX ?? spatialFeature.TBBHLX;
      spatialFeature.JCRQ = params.JCRQ ?? spatialFeature.JCRQ;
      spatialFeature.JZRQ = params.JZRQ ?? spatialFeature.JZRQ;
      spatialFeature.JD = params.JD ?? spatialFeature.JD;
      spatialFeature.WD = params.WD ?? spatialFeature.WD;
      spatialFeature.SHENG = params.SHENG ?? spatialFeature.SHENG;
      spatialFeature.SHI = params.SHI ?? spatialFeature.SHI;
      spatialFeature.XIAN = params.XIAN ?? spatialFeature.XIAN;
      spatialFeature.XZQDM = params.XZQDM ?? spatialFeature.XZQDM;
      spatialFeature.WTFXRQ = params.WTFXRQ ?? spatialFeature.WTFXRQ;
      spatialFeature.ZYRY = params.ZYRY ?? spatialFeature.ZYRY;
      spatialFeature.ZJRY = params.ZJRY ?? spatialFeature.ZJRY;
      spatialFeature.WTLY = params.WTLY ?? spatialFeature.WTLY;
      spatialFeature.BZ = params.BZ ?? spatialFeature.BZ;
      spatialFeature.JZSXWXSJ = params.JZSXWXSJ ?? spatialFeature.JZSXWXSJ;
      spatialFeature.geom = params.geom ?? spatialFeature.geom;

      const result = await this.spatialFeatureRepository.save(spatialFeature);
      return {
        success: true,
        data: result,
        message: '更新空间特征成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '更新空间特征失败: ' + error.message,
      };
    }
  }

  // 删除空间特征
  async deleteSpatialFeature(params: SpatialFeatureDeleteDto) {
    const { id } = params;
    try {
      const spatialFeature = await this.spatialFeatureRepository.findOne({
        where: { id },
      });
      if (!spatialFeature) {
        return {
          success: false,
          message: '要删除的空间特征不存在',
        };
      }

      await this.spatialFeatureRepository.remove(spatialFeature);
      return {
        success: true,
        message: '删除空间特征成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '删除空间特征失败: ' + error.message,
      };
    }
  }
}
